package com.algorithm;

/**
 * @author: sun.hongliang
 * @create: 2019/11/16 11:50
 */
public class HapyTest {

    public static void main(String[] args) {

    }

    public boolean isHappy(int n) {
        int slow = hapySum(n);
        int fast = slow;
        do {
            slow = hapySum(slow);
            fast = hapySum(fast);
            fast = hapySum(fast);
        } while (fast != slow);
        return fast == 0;
    }

    private int hapySum(int n) {
        int sum = 0;
        while (n != 0) {
            int temp = n % 10;
            sum += temp * temp;
            n /= 10;
        }
        return sum;
    }
}
